﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.SessionState;


public class class_user
{
    class_database db;
    HttpSessionState ses = HttpContext.Current.Session;
    HttpRequest req = HttpContext.Current.Request;
    public bool checkExistUser(string _user)
    {
        db = new class_database();
        string sql = "SELECT * FROM tbl_user WHERE username= @name";
        DataTable dt = new DataTable();
        string[] arrayParamenter = new[] { "@name" };
        string[] arrayValues = new[] { _user };
        dt = db.GetDatatableWithParameters(sql, arrayParamenter, arrayValues);

        if (dt != null && dt.Rows.Count > 0)
            return true;
        else
            return false;
    }
    public bool checkUser(string _user, string _pass)
    {
        _user = class_string.clear_html(_user);
        _pass = class_string.clear_html(_pass);
        db = new class_database();
        _pass = class_string.MD5Hash(_pass);


        string sql = "SELECT * FROM user_tb WHERE username=@username AND password=@password AND status=1";

        DataTable dt = new DataTable();
        string[] arrayParamenter = new[] { "@username", "@password" };
        string[] arrayValues = new[] { _user, _pass };
        dt = db.GetDatatableWithParameters(sql, arrayParamenter, arrayValues);

            
        if (dt != null && dt.Rows.Count > 0)
        {
            ses["username"] = _user;
            ses["keyhash"] = class_string.MD5Hash(DateTime.Now.ToLongTimeString());
            ses["id"] = dt.Rows[0]["id"];
            ses["user_type"] = dt.Rows[0]["user_type"];
            
         /*   string[] result = new string[2];
            result[0] = Convert.ToString(dt.Rows[0]["id"]);
            result[0] = Convert.ToString(dt.Rows[0]["user_type"]);
            */

            return true; // Tồn tại
        }
        else
        {
            ses["id"] = "";
            ses["password1"] = "";
            ses["keyhash"] = "";
            ses["user_type"] = "";
            return false; // Không tồn tại
        }
    }

    public bool LoginUser(string _user)
    {
        _user = class_string.clear_html(_user);       
        db = new class_database();

        string sql = "SELECT * FROM tbl_user WHERE name=@username AND status=1";

        DataTable dt = new DataTable();
        string[] arrayParamenter = new[] { "@username"};
        string[] arrayValues = new[] { _user };
        dt = db.GetDatatableWithParameters(sql, arrayParamenter, arrayValues);

        if (dt != null && dt.Rows.Count > 0)
        {
            
            ses["username"] = _user;
            ses["keyhash"] = class_string.MD5Hash(DateTime.Now.ToLongTimeString());
            ses["id"] = dt.Rows[0]["id"];
            ses["user_type"] = dt.Rows[0]["type"];          

            return true; // Tồn tại
        }
        else
        {
            ses["id"] = "";            
            ses["keyhash"] = "";
            ses["user_type"] = "";
            return false; // Không tồn tại
        }
    }
    public string GetIpUser()
    {
        string ip;
        ip = req.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (!string.IsNullOrEmpty(ip))
        {
            string[] ipRange = ip.Split(',');
            int le = ipRange.Length - 1;
            string trueIP = ipRange[le];
        }
        else
        {
            ip = req.ServerVariables["REMOTE_ADDR"];
        }

        if (ip == null || ip == "0.0.0.0")
        {
            ip = req.UserHostAddress;
        }
        return ip;
    }
}